import requests
import os
import json
import random
import time
import typing
from data import DataSource

# 用于标识流程的提前结束
class FlowDone(Exception):
  def __init__(self, *args: object) -> None:
    super().__init__(*args)


def getJob() -> dict[str, typing.Any]:
  
  # 用户可能更改配置,导致indices发生变化, 需要检查job中是否缺少indices中的键值对
  if not os.path.exists(".job"):
    open(".job","w",encoding="utf8").close()
  
  with open(".job", "r", encoding="utf8") as f:
    s = f.read()
    
    job = None
    try:
      job = json.loads(s)
    except Exception:
      job = {}

    data = DataSource.get_indices()
    cities = data["cities"]
    industries = data["industries"]
    positions = data["positions"]    
    for c in cities:
      city = c[1]
      if job.get(city) == None:
        job[city] = {}
        
      for i in industries:
        industry = i[1]
        if job[city].get(industry) == None:
          job[city][industry] = {}
          
        for p in positions:
          positionType = p[1]
          position = p[3]
          if job[city][industry].get(positionType) == None:
            job[city][industry][positionType] = {}
          
          if job[city][industry][positionType].get(position) == None:
            job[city][industry][positionType][position] = {
              "finish":False,
              "curPage":1
            } 
  return job
    


    